﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Mars.Got;

namespace Yoozoo.Core.Common
{
    public class ResolutionQualityListener : 
        QualityListenerMono,
        IRenderResolutionAdjuster
    {
        public int minScreenWidth = 1280;
        public int maxScreenWidth = 2560;

        public int screenWidth;
        public int screenHeight;

        public int trySetWidth;
        public int trySetHeight;

        public float scaleRatio = 1.0f;


        private static int originScreenWidth;
        private static int originScreenHeight;

        [Range(0.5f,1f)]
        public float minScreenWidthScale = 0.75f;
        [Range(0.5f,1f)]
        public float minScreenHeightScale = 0.75f;
        
        private void Awake()
        {
            // 在未设置分辨率之前拿到原始分辨率
            originScreenWidth = Screen.width;
            originScreenHeight = Screen.height;
            if (originScreenWidth < originScreenHeight)
            {
                originScreenWidth = Screen.height;
                originScreenHeight = Screen.width;
            }
        }

        // [Serializable]
        // public class ResolutionQualitySetting : QualitySetting
        // {
        //     [Range(360,2048)]
        //     public int maxScreenWidth = 1080;
        //
        //     public void Apply(out int sWidth,out int sHeight)
        //     {
        //         ResolutionQualityListener.ChangeRes(maxScreenWidth,out sWidth,out sHeight);
        //     }
        // }

        private void ChangeRes(float resolutionPercent, int maxScreenWidth, int minScreenWidth, out int sWidth,out int sHeight)
        {
            int screenWidth = originScreenWidth;
            int screenHeight = originScreenHeight;
            
            float zoomRate = resolutionPercent * 0.01f;
            zoomRate = Mathf.Min(zoomRate, 1);
            
            sWidth = Mathf.RoundToInt(screenWidth * zoomRate);
            sHeight = Mathf.RoundToInt(screenHeight * zoomRate);
            
            int limitWidth = Mathf.Min(Mathf.Max(sWidth, minScreenWidth),maxScreenWidth);
            float r = limitWidth * 1.0f / sWidth;
            sWidth = (int)(sWidth * r);
            sHeight = (int)(sHeight * r);

            // 改分辨率只改到一定比例，其它交给RenderScale，这里是为了UI不模糊
            int sWidthLimited = Mathf.Max((int)(originScreenWidth * minScreenWidthScale), sWidth);
            int sHeightLimited = Mathf.Max((int)(originScreenHeight * minScreenHeightScale), sHeight);
            if (Screen.orientation == ScreenOrientation.LandscapeLeft)
            {
                Screen.SetResolution(sWidthLimited, sHeightLimited, true);
            }
            else
            {
                Screen.SetResolution(sHeightLimited,sWidthLimited, true);
            }
            
        }
        
        // public override void ChangeQualitySetting(ResolutionQualitySetting qualitySetting)
        // {
        //     qualitySetting.Apply(out trySetWidth,out trySetHeight);
        //     screenWidth = Screen.width;
        //     screenHeight = Screen.height;
        // }

        public void ChangeResolution(float resolutionPercent)
        {
#if UNITY_STANDALONE_WIN
            // PC版不降分辨率，设置里面直接设       
#else
            ChangeRes(resolutionPercent,maxScreenWidth, minScreenWidth,out trySetWidth,out trySetHeight);

            screenWidth = Screen.width;
            screenHeight = Screen.height;

            scaleRatio = Mathf.Min(trySetWidth * 1.0f / screenWidth,1.0f);
            
            Debug.Log($"设置分辨率，screenWidth : {screenWidth},screenHeight : {screenHeight} || trySetWidth : {trySetWidth},trySetHeight : {trySetHeight} || scaleRatio: {scaleRatio}");
#endif
        }
    }
}
